﻿Imports OMANE.Common
Imports OMANE.Common.UI
Imports System.Collections.ObjectModel

Public Class ModuleDashboarding
    Implements IModule

    Friend Shared _rootnode As V2ViewModelTabGroup
    Friend Shared _datamodelnode As V2ViewModelTabGroup
    Friend Shared _datamodelimportnode As V2ViewModelTabGroup
    Friend Shared _activeproject As Project
    Friend Shared _uiservice As IUIService
    Friend Shared _datamodelindicateurnode As V2ViewModelTabGroup
    Friend Shared _dashboardmodels As V2ViewModelTabGroup
    Friend Shared _zonetypes As Dictionary(Of String, Type)
    Friend Shared _propertyGridDictionary As PropertyGridDictionary
    Dim _ressources As V2ViewModelTabGroup
    Friend Shared _undoredoservice As IUndoRedoService
    Friend Shared _dashboardmenuitem As ViewModelMainMenuItem
    Friend Shared _resdic As ResDictionary
    Dim _projectsmenuitem As ViewModelMainMenuItem
    Dim _datamodelstructurenode As V2ViewModelTabGroup

    Public ReadOnly Property Dependencies As IEnumerable(Of ModuleReference) Implements IModule.Dependencies
        Get

        End Get
    End Property

    Public Sub Load() Implements IModule.Load

    End Sub

    Public Sub Reset(Application As Application) Implements IModule.Reset

        'Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(Infragistics.Windows.Chart.XamChart))("Axes")
        'Dim attr = CType(prop.Attributes(GetType(ComponentModel.DesignerSerializationVisibilityAttribute)), ComponentModel.DesignerSerializationVisibilityAttribute)
        'Dim visfinfo = GetType(ComponentModel.DesignerSerializationVisibilityAttribute).GetField("visibility", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
        'visfinfo.SetValue(attr, ComponentModel.DesignerSerializationVisibility.Content)
        'Dim attrvis = attr.Visibility

        Dim prop = ComponentModel.TypeDescriptor.GetProperties(GetType(Windows.Controls.ListBox))("Items")
        Dim attr = CType(prop.Attributes(GetType(ComponentModel.DesignerSerializationVisibilityAttribute)), ComponentModel.DesignerSerializationVisibilityAttribute)
        Dim visfinfo = GetType(ComponentModel.DesignerSerializationVisibilityAttribute).GetField("visibility", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
        visfinfo.SetValue(attr, ComponentModel.DesignerSerializationVisibility.Hidden)
        Dim attrvis = attr.Visibility


        _activeproject = New Project()

        _uiservice = Application.GetService(Of IUIService)()
        _resdic = New ResDictionary()
        _uiservice.AddResourceDictionary(_resdic)


        _dashboardmenuitem = New ViewModelMainMenuItem() With {.Title = "Tableaux de Bord"}
        _dashboardmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Configurer Partage", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewModelSharedSettings(), WindowTitle:="Parametres de partage", Width:=600, Height:=400)})
        _dashboardmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Annuler", .Command = Sub(o As Object) UndoRedoEngine.Instance.Undo()})
        _dashboardmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Retablir", .Command = Sub(o As Object) UndoRedoEngine.Instance.Redo()})

        _projectsmenuitem = New ViewModelMainMenuItem() With {.Title = "Projets"}

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If My.Settings.AllowUserEditDashboard Then
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Nouveau projet local", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Nouveau projet partagé", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Ouvrir un projet local", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Ouvrir un projet partagé", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Enregistrer le projet sous", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Enregistrer le projet", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
        Else
            _projectsmenuitem.Children.Add(New ViewModelMainMenuItem() With {.Title = "Ouvrir un projet partagé", .Command = Sub(o As Object) _uiservice.ShowMessage(New ViewSharedProjectLoad())})
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        _dashboardmenuitem.Children.Add(_projectsmenuitem)
        _uiservice.ViewModelMainMenu.Children.Add(_dashboardmenuitem)

        _undoredoservice = Application.GetService(Of IUndoRedoService)()


        _propertyGridDictionary = New PropertyGridDictionary()

        ' MsgBox(String.Join(vbCrLf, From k As System.Collections.DictionaryEntry In _propertyGridDictionary Select k.Key.GetType().FullName & ":" & k.Key.ToString()))


        Dim indics = _uiservice.V2ViewModelAccordeonMenu.Indicateurs.Body


        _rootnode = New V2ViewModelTabGroup() With {.Title = "Dashboarding"}

        _datamodelnode = New V2ViewModelTabGroup() With {.Title = "Modele de donnees", .TabGroup = New ObjectModel.ObservableCollection(Of IViewModelTabSection)}

        _datamodelimportnode = New V2ViewModelTabGroup() With {.Title = "Importation", .TabSection = New ViewModelDataSourceList(), .TabGroup = _datamodelnode.TabGroup}
        _datamodelimportnode.TabGroup.Add(_datamodelimportnode.TabSection)
        _datamodelnode.Children.Add(_datamodelimportnode)

        _datamodelstructurenode = New V2ViewModelTabGroup() With {.Title = "Structure", .TabSection = New ViewModelDataStructure(), .TabGroup = _datamodelnode.TabGroup}
        _datamodelstructurenode.TabGroup.Add(_datamodelstructurenode.TabSection)
        _datamodelnode.Children.Add(_datamodelstructurenode)

        _datamodelindicateurnode = New V2ViewModelTabGroup() With {.Title = "Indicateurs", .TabSection = New ViewModelIndicateur(), .TabGroup = _datamodelnode.TabGroup}
        _datamodelindicateurnode.TabGroup.Add(_datamodelindicateurnode.TabSection)
        _datamodelnode.Children.Add(_datamodelindicateurnode)

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If My.Settings.AllowUserEditDashboard Then
            _rootnode.Children.Add(_datamodelnode)
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        indics.Add(_rootnode)

        _ressources = New V2ViewModelTabGroup() With { _
          .Title = "Ressources", _
          .TabGroup = New ObservableCollection(Of IViewModelTabSection),
          .TabSection = New ViewModelRessources() With {.Title = "Ressources"}}

        _ressources.TabGroup.Add(_ressources.TabSection)

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If My.Settings.AllowUserEditDashboard Then
            _rootnode.Children.Add(_ressources)
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        _dashboardmodels = New V2ViewModelTabGroup() With { _
           .Title = "Tableaux de Bord", _
           .TabGroup = New ObservableCollection(Of IViewModelTabSection)}

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If My.Settings.AllowUserEditDashboard Then
            _dashboardmodels.ContextualCommands.Add( _
                        New ViewModelCommand("Ajouter un tableau de bord", _
                                New DelegateCommand(AddressOf AddDashboardExecute)))
            _dashboardmodels.ContextualCommands.Add( _
              New ViewModelCommand("Charger un tableau de bord", _
                      New DelegateCommand(AddressOf LoadDashboardExecute)))
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        _rootnode.Children.Add(_dashboardmodels)

        _zonetypes = New Dictionary(Of String, Type)
        _zonetypes.Add("Texte", GetType(TextBlockZone))
        _zonetypes.Add("Nombre", GetType(NumberZone))
        _zonetypes.Add("Graphe", GetType(XamChartZone))
        '_zonetypes.Add("Autre Graphe", GetType(WindowsFromsChartZone))
        _zonetypes.Add("Table", GetType(DataGridZone))
        _zonetypes.Add("Liste de choix", GetType(ListboxZone))
        '_zonetypes.Add("Liste deroulante de choix", GetType(ComboboxZone))
        '_zonetypes.Add("Zone de saisie", GetType(TextBoxZone))
        '_zonetypes.Add("Calendrier", GetType(CalendarZone))

    End Sub

    Public Sub Unload() Implements IModule.Unload

    End Sub

    Private Shared dashinc = 0
    Private Sub AddDashboardExecute()

        Dim layout = New ViewDashboardLayout()
        dashinc += 1
        layout.DashboardNameTextBlock.Text = "Dashboard " & dashinc
        _uiservice.ShowMessage(layout)

        Dim node = New V2ViewModelTabGroup()
        node.Icone = StandardIcons.TRAFFIC
        node.Title = layout.DashboardNameTextBlock.Text
        node.TabGroup = _dashboardmodels.TabGroup

        Dim dash = New ViewModelDashboard(CInt(layout.RowCountSlider.Value), CInt(layout.ColumnCountSlider.Value))
        dash._Name = "dashboard_" & Guid.NewGuid().ToString().ToLower().Replace("-", "")
        dash.Title = node.Title
        node.TabSection = dash
        node.TabGroup.Add(dash)

        node.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter une zone", _
                    New DelegateCommand(Sub() AddDashboardZoneExecute(node))))
        'node.ContextualCommands.Add( _
        '    New ViewModelCommand("Ajouter une grille", _
        '            New DelegateCommand(Sub() AddDashboardGridExecute(node))))
        'node.ContextualCommands.Add( _
        '    New ViewModelCommand("Ajouter un panneau", _
        '            New DelegateCommand(Sub() AddDashboardStackExecute(node))))
        node.ContextualCommands.Add( _
           New ViewModelCommand("Options", _
                   New DelegateCommand(Sub() DashboardOptionsExecute(node))))
        node.ContextualCommands.Add( _
          New ViewModelCommand("Enregistrer sous", _
                  New DelegateCommand(Sub() SaveDashboardAsExecute(node))))
        node.ContextualCommands.Add( _
          New ViewModelCommand("Enregistrer", _
                  New DelegateCommand(Sub() SaveDashboardExecute(node))))

        _dashboardmodels.Children.Add(node)
    End Sub

    Private Sub AddDashboardZoneExecute(node As V2ViewModelTabGroup)
        Dim dash As ViewModelDashboard = node.TabSection

        dash.AddNewDashboardZone()
    End Sub

    Private Sub LoadDashboardExecute()
        Dim opd = New Microsoft.Win32.OpenFileDialog()
        opd.Filter = "Fichiers Tableaux de Bord OMANE|*.otb;"

        If opd.ShowDialog() Then
            Dim x = XElement.Load(opd.FileName)

            Dim dash = New ViewModelDashboard(CInt(x.@RowCount), CInt(x.@ColumnCount))

            dash.Load(x)

            Dim node = New V2ViewModelTabGroup()
            node.Icone = StandardIcons.TRAFFIC
            node.Title = x.@Title
            node.TabGroup = _dashboardmodels.TabGroup

            node.TabGroup.Add(dash)
            node.TabSection = dash

            node.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter une zone", _
                    New DelegateCommand(Sub() AddDashboardZoneExecute(node))))
            'node.ContextualCommands.Add( _
            '    New ViewModelCommand("Ajouter une grille", _
            '            New DelegateCommand(Sub() AddDashboardGridExecute(node))))
            'node.ContextualCommands.Add( _
            '    New ViewModelCommand("Ajouter un panneau", _
            '            New DelegateCommand(Sub() AddDashboardStackExecute(node))))
            node.ContextualCommands.Add( _
               New ViewModelCommand("Options", _
                       New DelegateCommand(Sub() DashboardOptionsExecute(node))))
            node.ContextualCommands.Add( _
              New ViewModelCommand("Enregistrer sous", _
                      New DelegateCommand(Sub() SaveDashboardAsExecute(node))))
            node.ContextualCommands.Add( _
              New ViewModelCommand("Enregistrer", _
                      New DelegateCommand(Sub() SaveDashboardExecute(node))))

            _dashboardmodels.Children.Add(node)
        End If
    End Sub
    Private Sub SaveDashboardAsExecute(node As V2ViewModelTabGroup)
        Dim spd = New Microsoft.Win32.SaveFileDialog()
        spd.Filter = "Fichiers Tableaux de Bord OMANE|*.otb;"

        If spd.ShowDialog() Then
            Dim dash As ViewModelDashboard = node.TabSection

            dash.ExternalFileName = spd.FileName

            Dim x = <Dashboard/>
            dash.Save(x)
            x.Save(dash.ExternalFileName)
        End If

    End Sub

    Private Sub SaveDashboardExecute(node As V2ViewModelTabGroup)
        Dim dash As ViewModelDashboard = node.TabSection

        If String.IsNullOrEmpty(dash.ExternalFileName) Then
            SaveDashboardAsExecute(node)
            Return
        End If

        Dim x = <Dashboard/>
        dash.Save(x)
        x.Save(dash.ExternalFileName)
    End Sub

    Private Sub DashboardOptionsExecute(node As V2ViewModelTabGroup)

    End Sub

    Private Sub AddDashboardGridExecute(node As V2ViewModelTabGroup)
        Throw New NotImplementedException
    End Sub

    Private Sub AddDashboardStackExecute(node As V2ViewModelTabGroup)
        'Dim layout = New ViewDashboardZoneLayout()
        'Dim dash A wqs ViewModelDashboard = node.TabSection
        'layout.DashboardZoneNameTextBlock.Visibility = Windows.Visibility.Collapsed
        'layout.MaxRowCount = dash.RowCount
        'layout.MaxColumnCount = dash.ColumnCount
        'layout.RowCountSlider.Maximum = dash.RowCount
        'layout.RowSlider.Maximum = dash.RowCount - 1
        'layout.ColumnCountSlider.Maximum = dash.ColumnCount
        'layout.ColumnSlider.Maximum = dash.ColumnCount - 1
        'layout.DashboardZoneTypeCombobox.Visibility = Windows.Visibility.Collapsed

        'ModuleDashboarding._uiservice.ShowMessage(layout)

        '  Dim s = New Windows.Controls.StackPanel()
        'Dim z = New ViewDashboardZone()

        'Dim zt = CType(layout.DashboardZoneTypeCombobox.SelectedItem, KeyValuePair(Of String, Type)).Value

        'z.DataContext = Activator.CreateInstance(zt)

        'SetGridLayoutIgnoreSplitter( _
        '    z, _
        '    CInt(layout.RowSlider.Value), _
        '    CInt(layout.RowCountSlider.Value), _
        '    CInt(layout.ColumnSlider.Value), _
        '    CInt(layout.ColumnCountSlider.Value))

        '_EditingGrid.Children.Add(z)
    End Sub

End Class

Public Class DelegateCommand
    Implements Windows.Input.ICommand

    Private _ExecuteMethod As Action(Of Object)


    Sub New(ExecuteMethod As Action(Of Object))
        Me._ExecuteMethod = ExecuteMethod
    End Sub
    Sub New(ExecuteMethod As Action)
        Me._ExecuteMethod = Sub(parameter As Object) ExecuteMethod()
    End Sub
    Public Function CanExecute(parameter As Object) As Boolean Implements Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Public Event CanExecuteChanged(sender As Object, e As EventArgs) Implements Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(parameter As Object) Implements Windows.Input.ICommand.Execute
        _ExecuteMethod(parameter)
    End Sub
End Class

Public Class UIObject
    Implements ComponentModel.INotifyPropertyChanged

    Protected Sub OnPropertyChanged(p As String)
        RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(p))
    End Sub

    Public Event PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Implements ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

Public Class IntToEnumConverter
    Implements Windows.Data.IValueConverter


    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.Convert
        Return CInt(value)
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.ConvertBack
        Return [Enum].ToObject(targetType, CInt(value))
    End Function
End Class